﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;

namespace TranTekBE.Marketing
{
    public partial class Marketing : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Load JS
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            if (Request.QueryString["vitri"] == null && Request.QueryString["doitac"] == null && Request.QueryString["loaiqc"] == null && Request.QueryString["start"] == null && Request.QueryString["end"] == null)
            {
                LoadData("", "", "", "", "");
            }
            else
            {
                LoadData(Request.QueryString["vitri"], Request.QueryString["doitac"], Request.QueryString["loaiqc"], Request.QueryString["start"], Request.QueryString["end"]);
            }
            if (!IsPostBack)
            {

                // Get vị trí
                FunctionsUtils.GetDropListViTriQC(droplist_vi_tri);
                // Get đối tác
                FunctionsUtils.GetDropListDoiTacQC(droplist_doi_tac);
                // Get loại qc
                FunctionsUtils.GetDropListLoaiQC(droplist_loai_quangcao);
                //
                droplist_vi_tri.SelectedValue = Request.QueryString["vitri"];
                droplist_doi_tac.SelectedValue = Request.QueryString["doitac"];
                droplist_loai_quangcao.SelectedValue = Request.QueryString["loaiqc"];
                //
                txt_time_start.Text = Request.QueryString["start"];
                txt_time_end.Text = Request.QueryString["end"];
            }

        }
        // Load data -------------------------------------------------------//
        void LoadData(string getvitri, string getdoitac, string getloaiqc, string getstart, string getend)
        {
            string sql_loc = @"SELECT *  FROM QuangCao
            Left Join DoiTac On DoiTac.DoiTacId = QuangCao.DoiTacId
            Left Join ViTriCuaHang On ViTriCuaHang.ViTriID = QuangCao.ViTriID
            Left Join LoaiQuangCao On LoaiQuangCao.LoaiQuangCaoId = QuangCao.LoaiQuangCaoId Where 1=1 ";
            string sql_orderby = "Order By QuangCao.StartDate desc";
            //
            string sql_vitri = "";
            string sql_doi_tac = "";
            string sql_loaiqc = "";
            string sql_start = "";
            string sql_end = "";


            if (getvitri != "")
            {
                sql_vitri = " AND QuangCao.ViTriID = '" + getvitri + "'";
            }

            if (getdoitac != "")
            {
                sql_doi_tac = " AND QuangCao.DoiTacId = '" + getdoitac + "'";
            }

            if (getloaiqc != "")
            {
                sql_loaiqc = " AND QuangCao.LoaiQuangCaoId = '" + getloaiqc + "'";
            }

            if (getstart != "")
            {
                sql_start = "AND QuangCao.StartDate >= '" + getstart + "'";
            }

            if (getend != "")
            {
                sql_end = "AND QuangCao.EndDate >= '" + getend + "'";
            }


            sql_loc += sql_vitri + sql_doi_tac + sql_loaiqc + sql_start + sql_end + sql_orderby;
            //

            DataSet ds = FunctionsUtils.Database2.ExecuteQuery(sql_loc);

            DataTable dt = ds.Tables[0];

            PagedDataSource pgitems = new PagedDataSource();

            System.Data.DataView dv = new System.Data.DataView(dt);

            pgitems.DataSource = dv;

            pgitems.AllowPaging = true;

            pgitems.PageSize = 10;

            pgitems.CurrentPageIndex = PageNumber;

            if (pgitems.PageCount > 1)
            {

                rptPages.Visible = true;

                System.Collections.ArrayList pages = new System.Collections.ArrayList();

                for (int i = 0; i < pgitems.PageCount; i++)

                    pages.Add((i + 1).ToString());

                rptPages.DataSource = pages;

                rptPages.DataBind();

            }

            else

                rptPages.Visible = false;

            rpDanhGia.DataSource = pgitems;

            rpDanhGia.DataBind();


        }

        public int PageNumber
        {

            get
            {

                if (ViewState["PageNumber"] != null)

                    return Convert.ToInt32(ViewState["PageNumber"]);

                else

                    return 0;

            }

            set
            {

                ViewState["PageNumber"] = value;

            }

        }

        protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;

            if (Request.QueryString["vitri"] == null && Request.QueryString["doitac"] == null && Request.QueryString["loaiqc"] == null && Request.QueryString["start"] == null && Request.QueryString["end"] == null)
            {
                LoadData("", "", "", "", "");
            }
            else
            {
                LoadData(Request.QueryString["vitri"], Request.QueryString["doitac"], Request.QueryString["loaiqc"], Request.QueryString["start"], Request.QueryString["end"]);
            }

        }
        // End load data -------------------------------------------------------//
        

        // Text Changed-----------------------------------------------------------------//
        protected void droplist_vi_tri_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Marketing.aspx?vitri=" + droplist_vi_tri.SelectedValue.ToString() + "&doitac=" + droplist_doi_tac.SelectedValue.ToString() + "&loaiqc=" + droplist_loai_quangcao.SelectedValue.ToString() + "&start=" + txt_time_start.Text.ToString() + "&end=" + txt_time_end.Text.ToString());
        }

        protected void droplist_doi_tac_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Marketing.aspx?vitri=" + droplist_vi_tri.SelectedValue.ToString() + "&doitac=" + droplist_doi_tac.SelectedValue.ToString() + "&loaiqc=" + droplist_loai_quangcao.SelectedValue.ToString() + "&start=" + txt_time_start.Text.ToString() + "&end=" + txt_time_end.Text.ToString());
        }

        protected void droplist_loai_quangcao_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Marketing.aspx?vitri=" + droplist_vi_tri.SelectedValue.ToString() + "&doitac=" + droplist_doi_tac.SelectedValue.ToString() + "&loaiqc=" + droplist_loai_quangcao.SelectedValue.ToString() + "&start=" + txt_time_start.Text.ToString() + "&end=" + txt_time_end.Text.ToString());
        }

        protected void txt_time_start_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect("Marketing.aspx?vitri=" + droplist_vi_tri.SelectedValue.ToString() + "&doitac=" + droplist_doi_tac.SelectedValue.ToString() + "&loaiqc=" + droplist_loai_quangcao.SelectedValue.ToString() + "&start=" + txt_time_start.Text.ToString() + "&end=" + txt_time_end.Text.ToString());
        }

        protected void txt_time_end_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect("Marketing.aspx?vitri=" + droplist_vi_tri.SelectedValue.ToString() + "&doitac=" + droplist_doi_tac.SelectedValue.ToString() + "&loaiqc=" + droplist_loai_quangcao.SelectedValue.ToString() + "&start=" + txt_time_start.Text.ToString() + "&end=" + txt_time_end.Text.ToString());
        }

        protected void btnAll_Click(object sender, EventArgs e)
        {
            Response.Redirect("Marketing.aspx");
            droplist_vi_tri.SelectedIndex = 0;
            droplist_doi_tac.SelectedIndex = 0;
            droplist_loai_quangcao.SelectedIndex = 0;
            txt_time_start.Text = "";
            txt_time_end.Text = "";
        }
        // End Text Changed--------------------------------------------------------------//
    }
}